package com.csc775.networking;

import java.io.*;
import java.net.*;
//import java.rmi.ServerException;
//import java.rmi.ServerException;



public class ServerThread extends Thread {
		private Socket TCPsocket   = null;
	   private Server server = null;
	   private int ID = -1;
	   private DataInputStream streamIn =  null;
	   private ObjectOutputStream streamOut =  null;
	   private ObjectOutputStream out;
		private ObjectInputStream in;
		private String message;
		private DatagramSocket UDPsocket;
		
	   public ServerThread(Server server, Socket socket, int id, DatagramSocket soc)
	   {  
		   this.server = server;  
		   this.TCPsocket = socket;  
		   this.ID = id;
		   this.UDPsocket=soc;
	   }
	   
	   @Override
	public void run()
	   {  
		   System.out.println("Server Thread " + ID + " running.");
		 
		   //sendMessage("Select Group/Groups");
		   recevMsg(); 
		 //  server.addToGroup(ID, message);  //adds user to group			
		//   UDPSendRcv();  //sends/receives UDP packet
		   
	   }
	   
	   public void open() throws IOException
	   {  
		  // streamIn = new DataInputStream(new BufferedInputStream(TCPsocket.getInputStream()));
		 //  streamOut = new ObjectOutputStream(TCPsocket.getOutputStream());
		//   out = new ObjectOutputStream(TCPsocket.getOutputStream());
		//	out.flush();
			/*try{
				in = new ObjectInputStream(TCPsocket.getInputStream());
			}catch(Exception ex){
				
			}*/
	   }
	  
	   /**
	    * Sends & receives UDP packets
	    */
	   public void UDPSendRcv(){
		   byte[] receiveData = new byte[1024];
           byte[] sendData = new byte[1024];
           DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
           while(true)
              {
        	   try{
        		     UDPsocket.receive(receivePacket);
                   String sentence = new String( receivePacket.getData());
                 System.out.println("RECEIVED: " + sentence);
                 InetAddress IPAddress = receivePacket.getAddress();
                 int port = receivePacket.getPort();
                 String capitalizedSentence = sentence.toUpperCase();
                 sendData = capitalizedSentence.getBytes();
                 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                 UDPsocket.send(sendPacket);
                 System.out.println("Server sent UDP packet: "+ capitalizedSentence);
              
              }catch(Exception e){
            	  System.out.println(e);
              }
              }
	   }
	   
	  
	
	   public void message(Socket t){
		   try{
			  PrintWriter out = new PrintWriter(TCPsocket.getOutputStream(), true);
       /* ... and an input stream */
       BufferedReader in = new BufferedReader(new InputStreamReader(
                   TCPsocket.getInputStream()));
       /* stdin stream */
       BufferedReader stdIn = new BufferedReader(
               new InputStreamReader(System.in));

       String msg;

       /* loop reading messages from stdin, send them to the server 
        * and read the server's response */
       while ((msg = stdIn.readLine()) != null) {
           out.println(msg);
           System.out.println(in.readLine());
       }
	   }catch(Exception e){
		 System.out.println(e);  
	   }
	   }
	   
	   /**
	    * sends TCP data
	    * @param msg
	    */
	   void sendMessage(String msg)
		{
			try{
				out.writeObject(msg);
				out.flush();
				System.out.println("Sent(Server): " + msg);
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}

	   /**
	    * Receives TCP data
	    */
	   public void recevMsg(){
		   try{
					in = new ObjectInputStream(TCPsocket.getInputStream());
						String message = (String)in.readObject();
						System.out.println("from client>" + message);
			  // message = (String)in.readObject();
			//	System.out.println("Received from client:" + message);
		   
		   }catch (Exception e){
		   System.out.println(e);
	   }
	   }
	   public void close() throws IOException
	   {  if (TCPsocket != null)    TCPsocket.close();
	      if (streamIn != null)  streamIn.close();
	   }
	}
